package action;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

import javax.swing.JFrame;

import data.PracownikData;

public class SubmitActionListener implements ActionListener
{
	
	private PracownikData pd;
	private String n, i, em, t, s;
	public SubmitActionListener(PracownikData data) {
	this.pd = data;
	n = pd.getNazwisko();
	System.out.println("SubmitActionListener");
	System.out.println(pd.getNazwisko());
	System.out.println(n);
	i = data.getImie();
	em = data.getEmail();
	t = data.getTelefon();
	s = data.getStanowisko();
//...for each JMenuItem instance:
//menuItem.addActionListener(this);
//for each JRadioButtonMenuItem: 
//rbMenuItem.addActionListener(this);
//for each JCheckBoxMenuItem: 
//cbMenuItem.addItemListener(this);
}

public void actionPerformed(ActionEvent e)
{
  try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
     System.out.println("connected");
     //Statement statement = conn.createStatement();
     CallableStatement statement = conn.prepareCall("EXEC DodajPracownika ?, ?, ?, ?, ?"); 
     //PreparedStatement statement = conn.prepareStatement("INSERT INTO dbo.PRACOWNIK VALUES (?,?,?,?,?)");
     statement.setString(1,n);
     System.out.println(n);
     statement.setString(2,i);
     statement.setString(3,em);
     statement.setString(4,t);
     statement.setString(5,s);
     //PreparedStatement statement = conn.prepareStatement("INSERT INTO dbo.PRACOWNIK VALUES ('"+n+"', '"+i+"', '"+em+"', '"+t+"','"+s+"')");
     //PreparedStatement statement = conn.prepareStatement("INSERT INTO ABSI.dbo.PRACOWNIK VALUES ('Nazwisko', 'Imie', 'E-mail', 'Telefon', 'Stanowisko')");
     statement.execute();

     conn.close();
  } catch (Exception ex) {
     ex.printStackTrace();
  }
}


}
